package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/9/19 19:56
 * @Version 1.0
 */
@Controller
@RequestMapping("houseUser")
@SuppressWarnings({"unchecked", "rawtypes"})
public class HouseUserController extends BaseController {

    @Reference
    private HouseUserService houseUserService;

    @PreAuthorize("hasAuthority('house.editUser')")
    @RequestMapping("create")
    public String create(@RequestParam("houseId") Long houseId, Model model){

        model.addAttribute("houseId",houseId);
        return "houseUser/create";

    }

    @PreAuthorize("hasAuthority('house.editUser')")
    @RequestMapping("save")
    public String save(HouseUser houseUser){

        houseUserService.insert(houseUser);
        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('house.editUser')")
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable("id") Long id , Model model){

        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser",houseUser);

        return "houseUser/edit";

    }

    @PreAuthorize("hasAuthority('house.editUser')")
    @RequestMapping("update")
    public String update(HouseUser houseUser){

        houseUserService.update(houseUser);
        return "common/successPage";

    }

    @PreAuthorize("hasAuthority('house.editUser')")
    @RequestMapping("delete/{houseId}/{id}")
    public String delete(@PathVariable("id") Long id,@PathVariable("houseId") Long houseId){

        houseUserService.delete(id);
        return "redirect:/house/" + houseId;

    }

}
